﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Delta_util.Clases;

namespace Delta_editoresWeb.Editors
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CargadorImagen runat=server></{0}:CargadorImagen>")]
    public class CargadorImagen : FileUpload
    {
        
        public CargadorImagen()
        {
            this.CssClass = "Edicion";
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string RutaCarpeta
        {
            get
            {
                String s = (String)ViewState["Ruta"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Ruta"] = value;
            }
        }

        public void GuardandoDatoControlAntes(CargadorImagen objeto, object entidad)
        {

            string rutaGuardar = RutaCarpeta;
            rutaGuardar = objeto.Page.Server.MapPath(RutaCarpeta);
            System.IO.DirectoryInfo directorioGuardar = new System.IO.DirectoryInfo(rutaGuardar);

            if (!directorioGuardar.Exists)
                directorioGuardar.Create();
            
            if (objeto.HasFile)
            {
                if (!string.IsNullOrWhiteSpace(Text))
                {
                    System.IO.FileInfo infoFile = new System.IO.FileInfo(this.Page.Server.MapPath(Text));
                    if (infoFile.Exists)
                        infoFile.Delete();

                }
                Text = RutaCarpeta + (rutaGuardar.EndsWith("/") ? "" : "/") + objeto.PostedFile.FileName;
                objeto.PostedFile.SaveAs(directorioGuardar.FullName + @"\" + objeto.PostedFile.FileName);

            }
            else
                Text = "";


        }
        protected override void RenderContents(HtmlTextWriter output)
        {
            Image img = new Image() { Height=50,Width=50,ImageUrl=Text};
            string nombre_rel = "img_css_" + Comun.getMD5(this.ID);
            
            //this.Page.ClientScript.RegisterClientScriptInclude(this.Page.GetType(), "jcolorbox", this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Delta_editoresWeb.Plugins.js.jquery.colorbox-min.js"));
            Utilerias.ExecScript(this.Page, "$('." + nombre_rel + "').colorbox({rel:'" + nombre_rel + "'});", "scriptcolumnaEditor", true);
            output.Write("<link  rel='stylesheet' type='text/css' href='" + this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Delta_editoresWeb.Plugins.css.colorbox.css") + "' />");
            output.Write("<script languaje='javascript' type='text/javascript' src='" + this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Delta_editoresWeb.Plugins.js.jquery.colorbox-min.js") + "' ></script>");
            output.Write("<br/>");
            output.Write("<a class='" + nombre_rel + "' href='" + Text.Replace("~/", Utilerias.ObtenerHttpServer(this.Page)) + "'  >");
            img.RenderControl(output);
            output.Write("</a>");

            //this.RenderControl(output);
        }
    }
}
